package com.gm.lib.net;

import com.gm.lib.data.UserPreference;

import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.Random;

/**
 * Created by cx on 15/4/21.
 */
public class GMRequestParams extends JSONObject {


    public static final String KEY_SEQNUM = "seqnum";
    public static final String KEY_ver = "ver";
    public static final String KEY_uid = "uid";
    public static final String KEY_token = "token";
    public static final String KEY_data = "data";


    /**
     * 唯一序列号,格式 产品ID+UID+UNIX时间戳+6位随机数
     */
    private String seqnum;
    /**
     * 接口版本号
     */
    private String ver;
    /**
     * 用户ID(未登录用户为0)
     */
    private String uid;
    /**
     * // 用户认证token(未登录用户为空字符串)
     */
    private String token;
    /**
     * 产品ID
     */
    public static final String PRODUCT_ID = "GMCAMERAARD";


    public GMRequestParams(JSONObject  jsonBody) {
        seqnum = PRODUCT_ID + UserPreference.getInstance().getUserUid() + System.currentTimeMillis() + getRandom();
        ver = "2.0";
        uid= UserPreference.getInstance().getUserUid();
        token=UserPreference.getInstance().getToken();

        try {
            put(KEY_SEQNUM, getSeqnum());
            put(KEY_ver, getVer());
            put(KEY_uid, getUid());
            put(KEY_token, getToken());
            put(KEY_data,jsonBody);
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 取得6位随机数
     * @return
     */
    private String getRandom(){
        Random r = new Random();
        int x = r.nextInt(999999);
        DecimalFormat df = new DecimalFormat("000000");
        String str = df.format(x);
        return str;
    }


    public String getSeqnum() {
        return seqnum;
    }
    public String getVer() {
        return ver;
    }
    public String getUid() {
        return uid;
    }
    public String getToken() {
        return token;
    }


}
